{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "13300088-ba77-4ebb-b28a-44d101a2b9d4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:04:43.984467Z",
     "iopub.status.busy": "2022-08-24T00:04:43.984014Z",
     "iopub.status.idle": "2022-08-24T00:04:43.990112Z",
     "shell.execute_reply": "2022-08-24T00:04:43.989064Z",
     "shell.execute_reply.started": "2022-08-24T00:04:43.984416Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import cv2, os, glob, codecs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3261c159-93cd-49fe-a99f-20e6409df751",
   "metadata": {},
   "source": [
    "# 本地抽帧目录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a3a9d25a-fa99-4b16-86a9-9c60014934f4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:04:43.991484Z",
     "iopub.status.busy": "2022-08-24T00:04:43.991255Z",
     "iopub.status.idle": "2022-08-24T00:04:44.311675Z",
     "shell.execute_reply": "2022-08-24T00:04:44.310481Z",
     "shell.execute_reply.started": "2022-08-24T00:04:43.991466Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "if not os.path.exists('frames'):\n",
    "    os.mkdir('frames')\n",
    "    os.mkdir(os.path.join('frames', 'train'))\n",
    "    os.mkdir(os.path.join('frames', 'test'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbc37eeb-d9a7-4821-9d3b-05cb51437f48",
   "metadata": {},
   "source": [
    "# 对数据集抽帧"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ff21780e-9742-4679-aa1c-1001d247e30c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:04:45.975597Z",
     "iopub.status.busy": "2022-08-24T00:04:45.975091Z",
     "iopub.status.idle": "2022-08-24T00:04:45.982772Z",
     "shell.execute_reply": "2022-08-24T00:04:45.982191Z",
     "shell.execute_reply.started": "2022-08-24T00:04:45.975555Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "def extract_images(video_path, out_dir):\n",
    "    video_name = os.path.basename(video_path).split('.')[0]\n",
    "    cam = cv2.VideoCapture(video_path)\n",
    "    print(video_path)\n",
    "    frame_count = 1\n",
    "    while True:\n",
    "        successed, img = cam.read()\n",
    "        if not successed:\n",
    "            break\n",
    "        outfile = f'./{out_dir}/{video_name}-{frame_count:06}.jpg'\n",
    "        cv2.imwrite(outfile, img)\n",
    "        frame_count += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5774379c-abe2-43ef-bb0b-8af8c697640b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:04:45.984118Z",
     "iopub.status.busy": "2022-08-24T00:04:45.983861Z",
     "iopub.status.idle": "2022-08-24T00:04:46.050224Z",
     "shell.execute_reply": "2022-08-24T00:04:46.049282Z",
     "shell.execute_reply.started": "2022-08-24T00:04:45.984099Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "paths = glob.glob('./国产平台动作识别挑战赛初赛公开数据/train/*')\n",
    "paths.sort()\n",
    "train_video_path = [x for x in paths if not x.endswith('txt')]\n",
    "train_ann_path = [x for x in paths if x.endswith('txt')]\n",
    "\n",
    "paths = glob.glob('./国产平台动作识别挑战赛初赛公开数据/test/*')\n",
    "test_video_path = [x for x in paths if not x.endswith('txt')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "3f076adc-d7cf-4714-87dd-c78ffb7dc3fa",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-23T23:48:28.979798Z",
     "iopub.status.busy": "2022-08-23T23:48:28.979367Z",
     "iopub.status.idle": "2022-08-23T23:51:09.951557Z",
     "shell.execute_reply": "2022-08-23T23:51:09.951072Z",
     "shell.execute_reply.started": "2022-08-23T23:48:28.979750Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./国产平台动作识别挑战赛初赛公开数据/train/0YcWeE1932.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/1ohZ8MedRg.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/1slb3y745f.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/29VaYq3JXj.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/2U5KsDjNlL.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/2hKYb784F1.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/2qWj0x89uM.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/3EBsrIbeKm.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/47UXVNu5hC.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/6RH3WACdVw.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/6qSNznsKbo.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/6qY5nKveIH.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/7HfpyuXbWt.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/7Z9jbQ32BY.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/7oDS5VTukA.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/7rL98YaJMF.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/8COhStcRpX.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/8rlKvwyToi.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/8wYiABKuOQ.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/94cRHgsmPd.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/96v1SganDM.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/9VEAwtrl1N.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/9Y6Il7bTZQ.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/9fd7jaxUNm.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/9xeY6shFGu.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/A0LdGs6SR9.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/A1yDVu73xW.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/AMV45LHIF7.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/AS5xktzDys.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/AuvRyPH6rz.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/B4LQZJy7Us.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/BU0yI9EuJZ.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/BWnhrGEVpI.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/Be2cHQLDI7.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/BnUR7evwhT.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/C3zncbuo2E.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/C4fOpZio51.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/CST7ckGPWA.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/CcAZnToNSk.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/DTLdtHiK8Z.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/DXPY7eouZG.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/DjdaOWFxwA.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/EoJjWxc5UM.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/Es4dUnQW9m.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/F1rmMqWbZo.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/F4avIqn8Oo.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/FLWCJ0MRHo.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/FXRVYMcxq9.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/FkWODydJN0.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/FlRvaQCu3P.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/GdAnog16j2.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/Grh6XQ3f4q.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/HBtYOqd0j2.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/HpTCVcM5nP.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/HriugxP3S9.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/HyIlYQ96fP.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/IjZP7KeQ9c.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/IwfEohCdUT.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/JgOG4SdCfo.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/JjrI3DOSo5.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/KRisZ0rXqS.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/KcfaroBePg.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/LJ0WXIEkdr.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/LUMrV2ckXZ.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/Lbv0Bc49tq.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/LoGpt9OiSh.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/MLP0Z9kJsz.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/OJbNDihxPW.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/Oi7TdnkgGX.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/P1KBt7cuNw.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/P5Qt3JajVq.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/PBZWlmVIbX.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/PCZiGHqyb9.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/PJLBIGgX0y.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/PmasHqtXLZ.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/Q0VWnHxAP9.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/QYf96HqvCJ.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/QakRXvwHp9.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/Qmpwg8bfsd.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/R51MasBmWD.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/R75Uz0jMHm.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/RE3cY27OAD.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/RF4DnUuMSr.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/RJxseNHIhg.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/RTiJumqE7r.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/Rb8ZKLBJTP.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/Rx3G9qF6iC.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/SRjIdvfDFo.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/SdAHZBOsCM.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/SjHAEVCvJh.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/Sml9C5Nfdj.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/SzWZiJmFQ8.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/TIAhH7S12W.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/TWiIzagoYD.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/ThAMYDPBbj.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/Twu64r27Ve.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/VCcOK8woxv.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/VFfQ4w2mNs.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/VQ9IPEzybx.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/ViG3vt8cwB.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/VjtENbJCZw.avi\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[mp3float @ 0x5575c1ec7680] Header missing\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./国产平台动作识别挑战赛初赛公开数据/train/VqBKAUi2GL.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/WZ0TF6dk1A.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/XVfF2b1Kqv.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/XegVm5U9jt.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/Y3ZAKifOso.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/YEToXdlBZy.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/Yp1FTlwS0a.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/Yvq1Mk8HZd.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/YyaAz6opOD.avi\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[mp3float @ 0x5575c1ec7680] Header missing\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./国产平台动作识别挑战赛初赛公开数据/train/ZJ9FRU12Vv.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/ZcYgpQVd41.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/ZoR4vSikuT.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/ZpDlyhEJCU.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/ZwdSBneRKF.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/Zy8e97vR3q.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/b3atQZrjck.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/b4LKifIjmH.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/bDjp6iuUmW.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/bEO6moSF7Q.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/br6dxqFB0f.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/btWCZV4cdf.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/bzBNCd6Wwh.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/cAFqf14JuD.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/ch2STwEoZ1.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/deUJmylVMq.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/e6TlwEs3ky.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/eXgH9WLl3y.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/efxpZ1TNYi.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/epQSUE4xI8.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/g38xIFA4JY.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/g78Yz02hUG.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/gZwdlomMRv.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/h2VGWrvpTQ.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/hGEAs4amoR.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/hQF5SOxvCt.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/hWZob4cTQq.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/hbY3DJ16Xn.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/hwOJaIxWKp.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/j2vdVbq0Ky.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/jIOohYJi0q.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/jWvtCiYlVz.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/jg8hU1XEzY.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/jk451wNU7W.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/kKhaO8vEWr.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/kNSozIRp0u.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/kRC6WjUQ8c.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/l942VaNw0D.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/lQ8Mdi3LrY.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/mtb6MB5nX1.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/n3CMeEx1JK.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/nChNdOqZM9.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/nevud1k5PH.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/o4m7X19ArM.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/oWLDe3mZfM.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/otM4vHpa3G.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/pVACkZFEHy.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/pbN5TALZzM.avi\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[mp3float @ 0x5575c1e4b8c0] Header missing\n",
      "[mp3float @ 0x5575c1e4b8c0] Header missing\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./国产平台动作识别挑战赛初赛公开数据/train/pmXWrj41o2.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/przQZUuGfE.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/qnrj05CIMv.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/rBiN3AFOXu.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/rGkV1KSy2m.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/rKS9liBxIU.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/rsBcHP7VFU.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/rwWdjkO4Gf.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/sWXPgu8x65.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/skYaL1poD2.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/tIc3Xe84lz.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/u9z6hSyXma.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/uGhZAV4WQI.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/udtLCFXe51.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/v0hjdDwXWp.mp4\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[mp3float @ 0x5575c1e4b8c0] Header missing\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./国产平台动作识别挑战赛初赛公开数据/train/vk0SrQhqXC.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/vryx760X1Y.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/wPjOR9cr0E.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/wT9Z1Be5cl.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/xME6lDO4ip.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/xmI0B4LJja.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/xnzR2j8EfQ.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/xqkRtbT45K.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/yBTrJZcX06.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/yLQE9gtpvA.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/yTgiQa4ZCE.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/zFdfgMejnG.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/zTe4dJ56I9.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/train/zxvp9DRNUQ.avi\n"
     ]
    }
   ],
   "source": [
    "for path in train_video_path:\n",
    "    extract_images(path, os.path.join('frames', 'train'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "cd7a2f71-3536-45f3-81a0-33d659655844",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-23T23:51:55.978670Z",
     "iopub.status.busy": "2022-08-23T23:51:55.978104Z",
     "iopub.status.idle": "2022-08-23T23:53:18.710656Z",
     "shell.execute_reply": "2022-08-23T23:53:18.709861Z",
     "shell.execute_reply.started": "2022-08-23T23:51:55.978620Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./国产平台动作识别挑战赛初赛公开数据/test/gV1Na54CQr.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/TQoVUbEr2g.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/rU3dgsTPqO.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/BjKa0AuLDw.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/aqZOboWNw5.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/4rc1IWzj3Z.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/XYg0vm7QGt.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/HylPkzgXSI.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/8xlqSPuYnd.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/nU1jsEHF7B.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/zsTyacKp4S.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/nzsqWCIiat.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/KRwfEdhYTJ.avi\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[mp3float @ 0x5575c1e4e840] Header missing\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./国产平台动作识别挑战赛初赛公开数据/test/yDkFrsx01P.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/cRp0S2UEwV.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/kep91dIiqb.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/Gg3sVkhRQB.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/4FIQVevj78.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/dbXTktUjF7.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/NQja1WinV7.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/R3yDhO1lra.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/vILEZozpQt.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/xOWyuTlMiR.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/i5lyd2KTzm.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/v5rZqYBo2f.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/eKAh0wudCz.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/Jg8YyBKbAD.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/Ic9AzqTiVh.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/eQJBEzq2Vv.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/RdzJDEISUc.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/wX8DMJ1lj9.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/OJu7wH3fr9.mp4\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[mp3float @ 0x5575c1e4e840] Header missing\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./国产平台动作识别挑战赛初赛公开数据/test/Sy1KwdiWGT.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/A1TrWPYLfQ.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/dIiynZS8Um.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/kJNdfHbjlp.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/ZJxz4hpa2q.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/dWvGzSF8M2.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/GdQ1C6wIba.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/BuDI69KeHX.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/s9PQFfhBTZ.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/Uex54hMsOQ.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/oRhdmk0FWr.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/AIepXGFEP9.mp4\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[mp3float @ 0x5575c1deb680] Header missing\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./国产平台动作识别挑战赛初赛公开数据/test/gCLJyovRnD.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/8Ro3GVMOaJ.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/w9YOKbFTZh.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/YlP40O5mar.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/ojLXhe9v3m.mp4\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[mp3float @ 0x5575c1deb240] Header missing\n",
      "[mp3float @ 0x5575c1deb240] Header missing\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "./国产平台动作识别挑战赛初赛公开数据/test/xXp07CFDot.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/J8ODbIgTVj.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/2NDsvznZQh.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/WOAtuphrc0.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/8QzTDsHlU2.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/0rBkb8ez5t.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/l2dpbOL7UD.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/zrjkb3anxW.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/TgCXvybS5Z.mp4\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/s0SDbrLcQI.avi\n",
      "./国产平台动作识别挑战赛初赛公开数据/test/XzhIofa0eA.avi\n"
     ]
    }
   ],
   "source": [
    "for path in test_video_path:\n",
    "    extract_images(path, os.path.join('frames', 'test'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ba67b33-3c4a-47b1-96be-e34c915ae93e",
   "metadata": {},
   "source": [
    "# 图像分类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "23cef875-c24a-4b04-ba43-991ca95d4950",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:04:50.072118Z",
     "iopub.status.busy": "2022-08-24T00:04:50.071692Z",
     "iopub.status.idle": "2022-08-24T00:04:50.083396Z",
     "shell.execute_reply": "2022-08-24T00:04:50.082734Z",
     "shell.execute_reply.started": "2022-08-24T00:04:50.072070Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "%pylab is deprecated, use %matplotlib inline and import the required libraries.\n",
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    }
   ],
   "source": [
    "from torch.utils.data.dataset import Dataset\n",
    "from torch.autograd import Variable\n",
    "import torch.optim as optim\n",
    "import torch.nn.functional as F\n",
    "import torch.nn as nn\n",
    "import torchvision.datasets as datasets\n",
    "import torchvision.transforms as transforms\n",
    "import torchvision.models as models\n",
    "\n",
    "import os\n",
    "import sys\n",
    "import glob\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from tqdm import tqdm\n",
    "\n",
    "%pylab inline\n",
    "\n",
    "import cv2\n",
    "from PIL import Image\n",
    "\n",
    "import torch\n",
    "torch.manual_seed(0)  # 减少随机性\n",
    "torch.backends.cudnn.deterministic = False  # 是否有确定性\n",
    "torch.backends.cudnn.benchmark = True  # 自动寻找最适合当前配置的高效算法，提高运行效率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5dfc7e73-9a34-4399-bfc3-a8ae9f636b93",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:04:50.977897Z",
     "iopub.status.busy": "2022-08-24T00:04:50.977447Z",
     "iopub.status.idle": "2022-08-24T00:04:50.985612Z",
     "shell.execute_reply": "2022-08-24T00:04:50.984985Z",
     "shell.execute_reply.started": "2022-08-24T00:04:50.977850Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "class XunFeiDataset(Dataset):\n",
    "    def __init__(self, img_path, img_label, transform=None):\n",
    "        self.img_path = img_path\n",
    "        self.img_label = img_label\n",
    "        if transform is not None:\n",
    "            self.transform = transform\n",
    "        else:\n",
    "            self.transform = None\n",
    "\n",
    "    def __getitem__(self, index):\n",
    "        img = cv2.imread(self.img_path[index])\n",
    "        img = img.astype(np.float32)\n",
    "        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
    "\n",
    "        if self.transform is not None:\n",
    "            img = self.transform(image=img)['image']\n",
    "        return img, torch.from_numpy(np.array(self.img_label[index]))\n",
    "\n",
    "    def __len__(self):\n",
    "        return len(self.img_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a6e1c818-5f40-4c66-b933-d4a0dee9b595",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:04:51.980669Z",
     "iopub.status.busy": "2022-08-24T00:04:51.980235Z",
     "iopub.status.idle": "2022-08-24T00:04:52.031320Z",
     "shell.execute_reply": "2022-08-24T00:04:52.030328Z",
     "shell.execute_reply.started": "2022-08-24T00:04:51.980620Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "train_img_list = []\n",
    "train_label_list = []\n",
    "\n",
    "for path in train_ann_path:\n",
    "    basename = os.path.basename(path)[:-4]\n",
    "    anns = codecs.open(path).readlines()\n",
    "    for idx, ann in enumerate(anns):\n",
    "        frame_count = idx + 1\n",
    "        train_img_list.append(f'./frames/train/{basename}-{frame_count:06}.jpg')\n",
    "        if ',' not in ann:\n",
    "            train_label_list.append(19)\n",
    "        else:\n",
    "            train_label_list.append(int(ann.split(',')[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "1a3b8e9b-8c36-479a-9a70-93b764eda050",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:05:32.176681Z",
     "iopub.status.busy": "2022-08-24T00:05:32.176079Z",
     "iopub.status.idle": "2022-08-24T00:05:32.195050Z",
     "shell.execute_reply": "2022-08-24T00:05:32.194482Z",
     "shell.execute_reply.started": "2022-08-24T00:05:32.176629Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "train_df = pd.DataFrame({\n",
    "    'path': train_img_list,\n",
    "    'label': train_label_list\n",
    "})\n",
    "train_df['label_int'], lbl = pd.factorize(train_df['label'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a0d4b13a-11f1-407f-b10a-89366b2a8b86",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:06:33.177159Z",
     "iopub.status.busy": "2022-08-24T00:06:33.176595Z",
     "iopub.status.idle": "2022-08-24T00:08:12.672839Z",
     "shell.execute_reply": "2022-08-24T00:08:12.671851Z",
     "shell.execute_reply.started": "2022-08-24T00:06:33.177110Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[ WARN:0@116.813] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/1ohZ8MedRg-000055.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@117.562] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/2U5KsDjNlL-000055.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@117.623] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/2hKYb784F1-000080.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@119.613] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/47UXVNu5hC-000114.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@121.993] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/7Z9jbQ32BY-000190.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@122.270] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/7rL98YaJMF-000080.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@122.680] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/94cRHgsmPd-000095.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@122.808] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/9VEAwtrl1N-000079.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@124.307] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/A0LdGs6SR9-000076.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@126.158] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/B4LQZJy7Us-000080.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@133.805] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/C3zncbuo2E-000079.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@134.440] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/CcAZnToNSk-000082.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@136.199] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/Es4dUnQW9m-000080.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@137.765] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/FLWCJ0MRHo-000111.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@137.827] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/FXRVYMcxq9-000080.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@138.136] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/FlRvaQCu3P-000079.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@144.826] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/HyIlYQ96fP-000055.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@144.845] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/IjZP7KeQ9c-000026.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@152.557] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/LUMrV2ckXZ-000106.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@153.447] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/OJbNDihxPW-000075.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@153.538] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/Oi7TdnkgGX-000106.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@153.594] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/P1KBt7cuNw-000078.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@153.658] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/P5Qt3JajVq-000079.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@155.456] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/Q0VWnHxAP9-000156.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@157.674] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/Qmpwg8bfsd-000079.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@158.618] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/RF4DnUuMSr-000080.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@160.710] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/SRjIdvfDFo-000054.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@160.775] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/SdAHZBOsCM-000080.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@161.087] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/Sml9C5Nfdj-000083.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@161.183] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/SzWZiJmFQ8-000095.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@165.430] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/VqBKAUi2GL-000079.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@165.634] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/XVfF2b1Kqv-000080.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@165.740] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/Y3ZAKifOso-000079.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@166.951] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/ZJ9FRU12Vv-000081.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@168.028] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/b3atQZrjck-000114.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@168.102] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/b4LKifIjmH-000092.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@168.222] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/bEO6moSF7Q-000077.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@168.340] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/btWCZV4cdf-000078.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@178.258] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/eXgH9WLl3y-000079.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@178.942] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/g78Yz02hUG-000097.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@190.931] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/hGEAs4amoR-000079.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@190.971] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/hQF5SOxvCt-000054.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@192.817] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/jWvtCiYlVz-000095.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@193.846] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/kKhaO8vEWr-000080.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@194.386] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/kRC6WjUQ8c-000080.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@195.491] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/mtb6MB5nX1-000108.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@196.404] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/nevud1k5PH-000049.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@198.660] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/pVACkZFEHy-000110.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@201.281] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/rBiN3AFOXu-000049.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@201.396] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/rKS9liBxIU-000103.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@206.040] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/tIc3Xe84lz-000076.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@206.605] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/uGhZAV4WQI-000078.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@211.928] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/xME6lDO4ip-000067.jpg'): can't open/read file: check file path/integrity\n",
      "[ WARN:0@214.601] global /io/opencv/modules/imgcodecs/src/loadsave.cpp (239) findDecoder imread_('./frames/train/yTgiQa4ZCE-000113.jpg'): can't open/read file: check file path/integrity\n"
     ]
    }
   ],
   "source": [
    "# 筛选非空的图片\n",
    "train_df = train_df[train_df['path'].apply(lambda x: cv2.imread(x) is not None)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "90c0ebf9-f9a4-4c52-92aa-b490388eee73",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:08:17.182793Z",
     "iopub.status.busy": "2022-08-24T00:08:17.182243Z",
     "iopub.status.idle": "2022-08-24T00:08:17.188924Z",
     "shell.execute_reply": "2022-08-24T00:08:17.188443Z",
     "shell.execute_reply.started": "2022-08-24T00:08:17.182744Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(34337, 3)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "43ed16f6-9957-4b9f-b53c-1dd2f1d7ff9b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:08:19.183586Z",
     "iopub.status.busy": "2022-08-24T00:08:19.183156Z",
     "iopub.status.idle": "2022-08-24T00:08:19.193624Z",
     "shell.execute_reply": "2022-08-24T00:08:19.193017Z",
     "shell.execute_reply.started": "2022-08-24T00:08:19.183538Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "train_df = train_df.sample(frac=1.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "e102cae8-110b-4ab7-bdc5-fcabe87b29ee",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:08:20.180946Z",
     "iopub.status.busy": "2022-08-24T00:08:20.180518Z",
     "iopub.status.idle": "2022-08-24T00:08:20.331948Z",
     "shell.execute_reply": "2022-08-24T00:08:20.331399Z",
     "shell.execute_reply.started": "2022-08-24T00:08:20.180899Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "model = models.resnet18(True)\n",
    "model.fc = nn.Sequential(\n",
    "    nn.Dropout(p=0.5, inplace=True),\n",
    "    nn.Linear(in_features=512, out_features=14, bias=True)\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "183281f9-3647-408f-94c8-93c6d9dd3a5e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:10:50.354345Z",
     "iopub.status.busy": "2022-08-24T00:10:50.353784Z",
     "iopub.status.idle": "2022-08-24T00:12:38.703547Z",
     "shell.execute_reply": "2022-08-24T00:12:38.702928Z",
     "shell.execute_reply.started": "2022-08-24T00:10:50.354298Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/lyz/.local/lib/python3.9/site-packages/albumentations/augmentations/transforms.py:1639: FutureWarning: RandomContrast has been deprecated. Please use RandomBrightnessContrast\n",
      "  warnings.warn(\n",
      "/home/lyz/.local/lib/python3.9/site-packages/albumentations/augmentations/transforms.py:1613: FutureWarning: This class has been deprecated. Please use RandomBrightnessContrast\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.1132761339098216 0.7755\n",
      "0.947293751463294 0.6825\n",
      "1.0231662292778492 0.7385\n",
      "1.0099382077902554 0.6845\n",
      "0.9304102680832148 0.6645\n"
     ]
    }
   ],
   "source": [
    "import albumentations as A\n",
    "from albumentations.pytorch import ToTensorV2\n",
    "\n",
    "def train(train_loader, model, criterion, optimizer):\n",
    "    model.train()\n",
    "    train_loss = 0.0\n",
    "    for i, (input, target) in enumerate(train_loader):\n",
    "        input = input.cuda(non_blocking=True)\n",
    "        target = target.cuda(non_blocking=True)\n",
    "\n",
    "        # compute output\n",
    "        output = model(input)\n",
    "        loss = criterion(output, target)\n",
    "\n",
    "        # compute gradient and do SGD step\n",
    "        optimizer.zero_grad()\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "        train_loss += loss.item()\n",
    "\n",
    "    return train_loss/len(train_loader)\n",
    "\n",
    "\n",
    "def validate(val_loader, model, criterion):\n",
    "    model.eval()\n",
    "\n",
    "    val_acc = 0.0\n",
    "\n",
    "    with torch.no_grad():\n",
    "        end = time.time()\n",
    "        for i, (input, target) in enumerate(val_loader):\n",
    "            input = input.cuda()\n",
    "            target = target.cuda()\n",
    "\n",
    "            # compute output\n",
    "            output = model(input)\n",
    "            loss = criterion(output, target)\n",
    "\n",
    "            val_acc += (output.argmax(1) == target).sum().item()\n",
    "\n",
    "    return val_acc / len(val_loader.dataset)\n",
    "\n",
    "\n",
    "def predict(test_loader, model, criterion):\n",
    "    model.eval()\n",
    "    val_acc = 0.0\n",
    "\n",
    "    test_pred = []\n",
    "    with torch.no_grad():\n",
    "        end = time.time()\n",
    "        for i, (input, target) in enumerate(test_loader):\n",
    "            input = input.cuda()\n",
    "            target = target.cuda()\n",
    "\n",
    "            # compute output\n",
    "            output = model(input)\n",
    "            test_pred.append(output.data.cpu().numpy())\n",
    "\n",
    "    return np.vstack(test_pred)\n",
    "\n",
    "\n",
    "# 随机拆分\n",
    "train_loader = torch.utils.data.DataLoader(\n",
    "    XunFeiDataset(train_df['path'].values[-2000:], train_df['label_int'].values[-2000:],\n",
    "                  A.Compose([\n",
    "                      A.Resize(300, 300),\n",
    "                      A.HorizontalFlip(p=0.5),\n",
    "                      A.VerticalFlip(p=0.5),\n",
    "                      A.RandomContrast(p=0.5),\n",
    "                      A.RandomBrightness(p=0.5),\n",
    "                      A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),\n",
    "                      ToTensorV2(),\n",
    "                      ])\n",
    "\n",
    "                  ), batch_size=10, shuffle=True, num_workers=4, pin_memory=False\n",
    ")\n",
    "\n",
    "val_loader = torch.utils.data.DataLoader(\n",
    "    XunFeiDataset(train_df['path'].values[-2000:], train_df['label_int'].values[-2000:],\n",
    "                  A.Compose([\n",
    "                      A.Resize(300, 300),\n",
    "                      A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),\n",
    "                      ToTensorV2(),\n",
    "                      ])\n",
    "                  ), batch_size=2, shuffle=False, num_workers=1, pin_memory=False\n",
    ")\n",
    "\n",
    "\n",
    "model = model.to('cuda')\n",
    "criterion = nn.CrossEntropyLoss().cuda()  # 自带softmax\n",
    "optimizer = torch.optim.SGD(model.parameters(), 0.005)\n",
    "scheduler = torch.optim.lr_scheduler.CyclicLR(optimizer, base_lr=0.001, max_lr=0.01, step_size_up=5, mode=\"triangular2\")\n",
    "\n",
    "best_acc = 0\n",
    "for _ in range(10):\n",
    "    train_loss = train(train_loader, model, criterion, optimizer)\n",
    "    val_acc = validate(val_loader, model, criterion)\n",
    "\n",
    "    if val_acc > best_acc:\n",
    "        torch.save(model.state_dict(), 'model.pth')\n",
    "        best_acc = val_acc\n",
    "\n",
    "    scheduler.step()\n",
    "    print(train_loss, val_acc)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1dd150e5-d4cc-493e-a130-46d057a15b7a",
   "metadata": {},
   "source": [
    "# 对测试集进行预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "84947ce0-3886-4898-9aba-c2d3c52162a4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:16:07.182744Z",
     "iopub.status.busy": "2022-08-24T00:16:07.182303Z",
     "iopub.status.idle": "2022-08-24T00:16:07.213264Z",
     "shell.execute_reply": "2022-08-24T00:16:07.212820Z",
     "shell.execute_reply.started": "2022-08-24T00:16:07.182695Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "test_img_list = glob.glob(os.path.join('frames', 'test') + '/*')\n",
    "test_img_list.sort()\n",
    "test_img_list = pd.DataFrame(test_img_list)\n",
    "test_img_list = test_img_list[test_img_list[0].apply(lambda x: cv2.imread(x) is not None)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "9767590d-2989-4cd8-a0ca-0672b2240c67",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:18:31.180149Z",
     "iopub.status.busy": "2022-08-24T00:18:31.179715Z",
     "iopub.status.idle": "2022-08-24T00:18:31.186910Z",
     "shell.execute_reply": "2022-08-24T00:18:31.186235Z",
     "shell.execute_reply.started": "2022-08-24T00:18:31.180100Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "test_loader = torch.utils.data.DataLoader(\n",
    "    XunFeiDataset(test_img_list[0].values, [0] * len(test_img_list),\n",
    "          A.Compose([\n",
    "              A.Resize(300, 300),\n",
    "              A.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),\n",
    "              ToTensorV2(),\n",
    "              ])\n",
    "          ), batch_size=10, shuffle=False, num_workers=1, pin_memory=False\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "98cdd160-e0bf-4ce9-bad9-7ac9707eb251",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:19:43.979803Z",
     "iopub.status.busy": "2022-08-24T00:19:43.979369Z",
     "iopub.status.idle": "2022-08-24T00:21:26.872174Z",
     "shell.execute_reply": "2022-08-24T00:21:26.871103Z",
     "shell.execute_reply.started": "2022-08-24T00:19:43.979753Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "test_pred = []\n",
    "for data, _ in test_loader:\n",
    "    pred = model(data.cuda())\n",
    "    test_pred += list(pred.argmax(1).cpu().numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "71949794-e92a-4a42-a044-6af21d212e31",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:21:46.020018Z",
     "iopub.status.busy": "2022-08-24T00:21:46.019582Z",
     "iopub.status.idle": "2022-08-24T00:21:46.028883Z",
     "shell.execute_reply": "2022-08-24T00:21:46.028354Z",
     "shell.execute_reply.started": "2022-08-24T00:21:46.019968Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "test_img_list['label'] = test_pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "b633c46a-1182-4d22-97d3-04d84a49c4fb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:21:46.981686Z",
     "iopub.status.busy": "2022-08-24T00:21:46.981128Z",
     "iopub.status.idle": "2022-08-24T00:21:47.003590Z",
     "shell.execute_reply": "2022-08-24T00:21:47.002905Z",
     "shell.execute_reply.started": "2022-08-24T00:21:46.981630Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "test_img_list['video'] =test_img_list[0].apply(lambda x: os.path.basename(x).split('-')[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "86add31e-12c4-43c4-b99a-84fe024be408",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:22:42.977372Z",
     "iopub.status.busy": "2022-08-24T00:22:42.976842Z",
     "iopub.status.idle": "2022-08-24T00:22:42.995175Z",
     "shell.execute_reply": "2022-08-24T00:22:42.994640Z",
     "shell.execute_reply.started": "2022-08-24T00:22:42.977320Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "test_img_list['label'] = test_img_list['label'].apply(lambda x: lbl[x])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "be64a4b3-db7a-443a-a733-f4eef3b31787",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:44:23.322924Z",
     "iopub.status.busy": "2022-08-24T00:44:23.322412Z",
     "iopub.status.idle": "2022-08-24T00:44:23.328210Z",
     "shell.execute_reply": "2022-08-24T00:44:23.327525Z",
     "shell.execute_reply.started": "2022-08-24T00:44:23.322875Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "if os.path.exists('labels'):\n",
    "    os.rmdir('labels')\n",
    "os.mkdir('labels')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "f3459a88-8f86-4a74-97ce-dac2f3474de3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:45:59.251211Z",
     "iopub.status.busy": "2022-08-24T00:45:59.250788Z",
     "iopub.status.idle": "2022-08-24T00:45:59.905652Z",
     "shell.execute_reply": "2022-08-24T00:45:59.904661Z",
     "shell.execute_reply.started": "2022-08-24T00:45:59.251164Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[mp3float @ 0x555cd0930700] Header missing\n",
      "[mp3float @ 0x555cd09de440] Header missing\n",
      "[mp3float @ 0x555cd09de440] Header missing\n",
      "[mp3float @ 0x555cd08f9600] Header missing\n",
      "[mp3float @ 0x555cd08f9600] Header missing\n"
     ]
    }
   ],
   "source": [
    "for path in test_video_path:\n",
    "    cam = cv2.VideoCapture(path)\n",
    "    length = int(cam.get(cv2.CAP_PROP_FRAME_COUNT))\n",
    "    \n",
    "    video_name = os.path.basename(path)[:-4]\n",
    "    df = test_img_list[test_img_list['video'] == video_name]\n",
    "    \n",
    "    with open(os.path.join('labels', video_name + '.txt'), 'w') as up:\n",
    "        for idx, row in enumerate(df.iterrows()):\n",
    "            if row[1]['label'] == 19:\n",
    "                up.write('{0}\\n'.format(idx))\n",
    "            else:\n",
    "                up.write('{0},{1}\\n'.format(idx, row[1]['label']))\n",
    "\n",
    "        if idx < length-1:\n",
    "            up.write('{0}\\n'.format(idx))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "id": "75b7fd8e-d3d2-43da-bdb1-61371c574f89",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:46:21.212037Z",
     "iopub.status.busy": "2022-08-24T00:46:21.211616Z",
     "iopub.status.idle": "2022-08-24T00:46:21.397348Z",
     "shell.execute_reply": "2022-08-24T00:46:21.395901Z",
     "shell.execute_reply.started": "2022-08-24T00:46:21.211989Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  adding: labels/ (stored 0%)\n",
      "  adding: labels/OJu7wH3fr9.txt (deflated 67%)\n",
      "  adding: labels/zrjkb3anxW.txt (deflated 65%)\n",
      "  adding: labels/A1TrWPYLfQ.txt (deflated 61%)\n",
      "  adding: labels/nzsqWCIiat.txt (deflated 71%)\n",
      "  adding: labels/Gg3sVkhRQB.txt (deflated 64%)\n",
      "  adding: labels/8Ro3GVMOaJ.txt (deflated 67%)\n",
      "  adding: labels/R3yDhO1lra.txt (deflated 67%)\n",
      "  adding: labels/BuDI69KeHX.txt (deflated 59%)\n",
      "  adding: labels/xOWyuTlMiR.txt (deflated 65%)\n",
      "  adding: labels/eKAh0wudCz.txt (deflated 64%)\n",
      "  adding: labels/wX8DMJ1lj9.txt (deflated 71%)\n",
      "  adding: labels/WOAtuphrc0.txt (deflated 66%)\n",
      "  adding: labels/s0SDbrLcQI.txt (deflated 62%)\n",
      "  adding: labels/NQja1WinV7.txt (deflated 66%)\n",
      "  adding: labels/dWvGzSF8M2.txt (deflated 67%)\n",
      "  adding: labels/l2dpbOL7UD.txt (deflated 70%)\n",
      "  adding: labels/kJNdfHbjlp.txt (deflated 67%)\n",
      "  adding: labels/dIiynZS8Um.txt (deflated 65%)\n",
      "  adding: labels/gV1Na54CQr.txt (deflated 65%)\n",
      "  adding: labels/XzhIofa0eA.txt (deflated 64%)\n",
      "  adding: labels/nU1jsEHF7B.txt (deflated 64%)\n",
      "  adding: labels/GdQ1C6wIba.txt (deflated 67%)\n",
      "  adding: labels/gCLJyovRnD.txt (deflated 65%)\n",
      "  adding: labels/TQoVUbEr2g.txt (deflated 64%)\n",
      "  adding: labels/4rc1IWzj3Z.txt (deflated 71%)\n",
      "  adding: labels/eQJBEzq2Vv.txt (deflated 63%)\n",
      "  adding: labels/ojLXhe9v3m.txt (deflated 71%)\n",
      "  adding: labels/4FIQVevj78.txt (deflated 71%)\n",
      "  adding: labels/kep91dIiqb.txt (deflated 71%)\n",
      "  adding: labels/BjKa0AuLDw.txt (deflated 61%)\n",
      "  adding: labels/aqZOboWNw5.txt (deflated 63%)\n",
      "  adding: labels/AIepXGFEP9.txt (deflated 70%)\n",
      "  adding: labels/rU3dgsTPqO.txt (deflated 67%)\n",
      "  adding: labels/w9YOKbFTZh.txt (deflated 65%)\n",
      "  adding: labels/XYg0vm7QGt.txt (deflated 66%)\n",
      "  adding: labels/KRwfEdhYTJ.txt (deflated 67%)\n",
      "  adding: labels/xXp07CFDot.txt (deflated 66%)\n",
      "  adding: labels/8QzTDsHlU2.txt (deflated 71%)\n",
      "  adding: labels/2NDsvznZQh.txt (deflated 69%)\n",
      "  adding: labels/Uex54hMsOQ.txt (deflated 64%)\n",
      "  adding: labels/Jg8YyBKbAD.txt (deflated 58%)\n",
      "  adding: labels/yDkFrsx01P.txt (deflated 64%)\n",
      "  adding: labels/HylPkzgXSI.txt (deflated 64%)\n",
      "  adding: labels/8xlqSPuYnd.txt (deflated 66%)\n",
      "  adding: labels/Sy1KwdiWGT.txt (deflated 66%)\n",
      "  adding: labels/zsTyacKp4S.txt (deflated 65%)\n",
      "  adding: labels/Ic9AzqTiVh.txt (deflated 67%)\n",
      "  adding: labels/0rBkb8ez5t.txt (deflated 64%)\n",
      "  adding: labels/YlP40O5mar.txt (deflated 60%)\n",
      "  adding: labels/J8ODbIgTVj.txt (deflated 62%)\n",
      "  adding: labels/i5lyd2KTzm.txt (deflated 60%)\n",
      "  adding: labels/s9PQFfhBTZ.txt (deflated 65%)\n",
      "  adding: labels/RdzJDEISUc.txt (deflated 70%)\n",
      "  adding: labels/TgCXvybS5Z.txt (deflated 69%)\n",
      "  adding: labels/oRhdmk0FWr.txt (deflated 64%)\n",
      "  adding: labels/ZJxz4hpa2q.txt (deflated 64%)\n",
      "  adding: labels/cRp0S2UEwV.txt (deflated 64%)\n",
      "  adding: labels/v5rZqYBo2f.txt (deflated 67%)\n",
      "  adding: labels/vILEZozpQt.txt (deflated 66%)\n",
      "  adding: labels/dbXTktUjF7.txt (deflated 70%)\n"
     ]
    }
   ],
   "source": [
    "!zip labels.zip labels/ -r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "id": "e5b5dcf3-cbca-4ccd-a490-10cabafbd404",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T00:46:11.979543Z",
     "iopub.status.busy": "2022-08-24T00:46:11.979117Z",
     "iopub.status.idle": "2022-08-24T00:46:12.074732Z",
     "shell.execute_reply": "2022-08-24T00:46:12.074216Z",
     "shell.execute_reply.started": "2022-08-24T00:46:11.979495Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n",
      "True\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[mp3float @ 0x555cd0377240] Header missing\n",
      "[mp3float @ 0x555cd0867000] Header missing\n",
      "[mp3float @ 0x555cd0655800] Header missing\n",
      "[mp3float @ 0x555cd08842c0] Header missing\n",
      "[mp3float @ 0x555cd08842c0] Header missing\n"
     ]
    }
   ],
   "source": [
    "for path in test_video_path:\n",
    "    cam = cv2.VideoCapture(path)\n",
    "    length = int(cam.get(cv2.CAP_PROP_FRAME_COUNT))\n",
    "    video_name = os.path.basename(path)[:-4]\n",
    "\n",
    "    txt_length = len(codecs.open(os.path.join('labels', video_name+'.txt')).readlines())\n",
    "    \n",
    "    print(length == txt_length)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0bc05971-8513-4672-b734-051d02275d44",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T01:54:36.128242Z",
     "iopub.status.busy": "2022-08-24T01:54:36.127819Z",
     "iopub.status.idle": "2022-08-24T01:54:36.158824Z",
     "shell.execute_reply": "2022-08-24T01:54:36.158252Z",
     "shell.execute_reply.started": "2022-08-24T01:54:36.128195Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import glob, os\n",
    "test_img_list = glob.glob(os.path.join('frames', 'test') + '/*')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3d91825e-cad6-4683-8a75-0980054a9a09",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T01:54:39.131419Z",
     "iopub.status.busy": "2022-08-24T01:54:39.130848Z",
     "iopub.status.idle": "2022-08-24T01:54:39.143869Z",
     "shell.execute_reply": "2022-08-24T01:54:39.143420Z",
     "shell.execute_reply.started": "2022-08-24T01:54:39.131369Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'frames/test/wX8DMJ1lj9-000944.jpg'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_img_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e03a3f44-bf33-41f2-90d6-6942c7294f15",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-08-24T01:54:46.129408Z",
     "iopub.status.busy": "2022-08-24T01:54:46.128813Z",
     "iopub.status.idle": "2022-08-24T01:54:46.249290Z",
     "shell.execute_reply": "2022-08-24T01:54:46.248577Z",
     "shell.execute_reply.started": "2022-08-24T01:54:46.129359Z"
    }
   },
   "outputs": [],
   "source": [
    "!cp frames/test/wX8DMJ1lj9-000944.jpg ./"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aeff7ed0-5222-4223-9add-bfbf542ab9da",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3.10"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
